<?php

namespace App\Kuafu\Dashboard\Service;

use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\Dashboard\Enum\DashboardCacheCodeEnum;
use App\Kuafu\Sys\Model\Menus;
use App\Kuafu\Sys\Service\MenusService;
use App\Kuafu\User\Model\Permissions;
use App\Kuafu\User\Model\RolePermissionMappings;
use App\Kuafu\User\Service\PermissionsService;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Base\Service\BaseService;
use Illuminate\Support\Facades\Redis;

class PosOnlineService extends BaseService
{

    /**
     * set pos 在线时间
     *
     * @param int $shopId
     * @param int $onlineTime
     * @return mixed
     */
    public static function setPosOnlineStatusTime(int $shopId, int $onlineTime = 600)
    {
        return Redis::connection('default')
                    ->set(DashboardCacheCodeEnum::getPosOnlineTimeCacheKey($shopId), time() + 600, 'ex', $onlineTime);
    }


    /**
     * 判断 pos 是否在线
     *
     * @param int $shopId
     * @return bool
     */
    public static function checkPosOnline(int $shopId)
    {
        try {
            $ttl = Redis::connection('default')
                        ->ttl(DashboardCacheCodeEnum::getPosOnlineTimeCacheKey($shopId));

            if ($ttl > 0) {
                return true;
            } else {
                return false;
            }
        } catch (\Exception $exception) {
            return false;
        }
    }
}
